<?php
namespace App\Http\Services;

use Illuminate\Support\Facades\DB;
use App\Lib\Publisher;
use App\Http\Model\Hint;
use App\Http\Model\User;
use App\Http\Model\Room;

//埋点上报
class ThinkingdataServices extends CommonServices
{
    private $userModel;
    protected $roomModel;

    public function __construct()
    {
        $this->userModel = new User();
        $this->roomModel = new Room();

    }

    /**
     * 埋点上报
     */
    public function buryPoint($userId, $data=[]) {
        return false;
        do {
            if(empty($userId) || empty($data['type'])) {
                break;
            }
            $type = '';
            $param = [];
            //进入房间
            if($data['type'] === 'into_room') {
                if(!empty($data['thoroughfare']) && !empty($data['room_id'])) {
                    $roomOpenId = $this->roomModel->getRoomOpenList($data['room_id'], 'roomOpenId');
                    $type = $data['type'];
                    $param = [
                        'thoroughfare' => $data['thoroughfare'],
                        'room_id' => $data['room_id'],
                        'user_id' => $userId,
                        'roomOpenId' => $roomOpenId,
                    ];
                }
            }
            //离开房间
            if($data['type'] === 'leave_room') {
                if(!empty($data['click_type']) && !empty($data['room_id'])) {
                    $roomOpenId = $this->roomModel->getRoomOpenList($data['room_id'], 'roomOpenId');
                    $type = $data['type'];
                    $param = [
                        'click_type' => $data['click_type'],
                        'room_id' => $data['room_id'],
                        'user_id' => $userId,
                        'roomOpenId' => $roomOpenId,
                    ];
                }
            }
            //房间内操作（听众）
            if($data['type'] === 'operate_ot') {
                if(!empty($data['action_type']) && !empty($data['room_id'])) {
                    $roomOpenId = $this->roomModel->getRoomOpenList($data['room_id'], 'roomOpenId');
                    $type = $data['type'];
                    $param = [
                        'action_type' => $data['action_type'],
                        'room_id' => $data['room_id'],
                        'user_id' => $userId,
                        'roomOpenId' => $roomOpenId,
                    ];
                }
            }

            $this->handleTrack($type, $param);
        } while (false);
    }

    /**
     * 埋点事件处理
     */
    public function handleTrack($type, $data) {
        return false;
        $now = time();
        do {
            $properties = [];
            //登录帐号
            if($type === 'login') {
                $properties['account_id'] = $data['user_id'];
                $properties['distinct_id'] = $this->userModel->getUserTdid($data['user_id']);
                $properties['is_first'] = $data['is_first'];//是否首次登录
            }
            //开启房间
            if($type === 'open_room') {
                $properties['account_id'] = $data['user_id'];
                $properties['distinct_id'] = $this->userModel->getUserTdid($data['user_id']);
                $properties['roomOpenId'] = $data['roomOpenId'];
            }
            //进入房间
            if($type === 'into_room') {
                $properties['account_id'] = $data['user_id'];
                $properties['distinct_id'] = $this->userModel->getUserTdid($data['user_id']);
                $properties['room_id'] = $data['room_id'];
                $properties['roomOpenId'] = $data['roomOpenId'];
                $properties['thoroughfare'] = $data['thoroughfare'];//进入通道
            }
            //离开房间
            if($type === 'leave_room') {
                $properties['account_id'] = $data['user_id'];
                $properties['distinct_id'] = $this->userModel->getUserTdid($data['user_id']);
                $properties['room_id'] = $data['room_id'];
                $properties['roomOpenId'] = $data['roomOpenId'];
                $properties['click_type'] = $data['click_type'];
            }
            //关闭房间
            if($type === 'close_room') {
                $properties['account_id'] = $data['user_id'];
                $properties['distinct_id'] = $this->userModel->getUserTdid($data['user_id']);
                $properties['room_id'] = $data['room_id'];
                $properties['roomOpenId'] = $data['roomOpenId'];
                $properties['host_user_id'] = $data['host_user_id'];
            }
            //房间内送礼
            if($type === 'give_present') {
                $properties['account_id'] = $data['user_id'];
                $properties['distinct_id'] = $this->userModel->getUserTdid($data['user_id']);
                $properties['room_id'] = $data['room_id'];
                $properties['roomOpenId'] = $data['roomOpenId'];
            }
            //房间内收礼
            if($type === 'get_present') {
                $properties['account_id'] = $data['user_id'];
                $properties['distinct_id'] = $this->userModel->getUserTdid($data['user_id']);
                $properties['room_id'] = $data['room_id'];
                $properties['roomOpenId'] = $data['roomOpenId'];
            }
            //房间内操作（听众）
            if($type === 'operate_ot') {
                $properties['account_id'] = $data['user_id'];
                $properties['distinct_id'] = $this->userModel->getUserTdid($data['user_id']);
                $properties['room_id'] = $data['room_id'];
                $properties['roomOpenId'] = $data['roomOpenId'];
                $properties['action_type'] = $data['action_type'];
            }
            //发起订单
            if($type === 'order_payment_start') {
                $properties['account_id'] = $data['user_id'];
                $properties['distinct_id'] = $this->userModel->getUserTdid($data['user_id']);
                $properties['order_id'] = $data['order_id'];
            }
            //订单支付完成
            if($type === 'order_payment_finish') {
                $properties['account_id'] = $data['user_id'];
                $properties['distinct_id'] = $this->userModel->getUserTdid($data['user_id']);
                $properties['order_id'] = $data['order_id'];
            }
            //邀请
            if($type === 'invite') {
                $properties['account_id'] = $data['user_id'];
                $properties['distinct_id'] = $this->userModel->getUserTdid($data['user_id']);
                $properties['invite_user'] = $data['invite_user'];
            }

            if(empty($properties)) {
                break;
            }
            $properties['now'] = $now;

            //MQ通知
            $publishObject = Publisher::getInstance();
            $publishObject->requestData = [
                'queue_route' => 'global.burypoint',
                'data' => [
                    'type' => $type,
                    'properties' => $properties,
                ],
            ];
            $publishObject->publisher();

        } while (false);
    }

}
